/**
 * 
 */
package com.googlecode.easyibatis.spring.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.easyibatis.core.dao.DAOSupport;
import com.googlecode.easyibatis.core.dao.annotation.InjectDAO;
import com.googlecode.easyibatis.core.exception.EasyIbatisException;
import com.googlecode.easyibatis.core.utils.Sort;
import com.googlecode.easyibatis.spring.dao.AbstractSupport;


/**
 * @author zxq
 * 
 */
public abstract class ServiceSupportImpl<T, PK extends Serializable> extends AbstractSupport {
	protected final org.apache.commons.logging.Log logger = LogFactory.getLog(this.getClass());

	@SuppressWarnings("unchecked")
	public DAOSupport<T, PK> getDAOSupport() throws EasyIbatisException {
		DAOSupport<T, PK> daoSupport = null;
		try {
			InjectDAO injectDAO = this.getClass().getAnnotation(InjectDAO.class);
			if (injectDAO != null && StringUtils.isNotBlank(injectDAO.daoName())) {
				daoSupport = getApplicationContext().getBean(injectDAO.daoName(), DAOSupport.class);
			} else {
				if (logger.isErrorEnabled()) {
					logger.error("WebApplicationContextUtils error");
				}
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error(e.getMessage(), e);
			}
			throw new EasyIbatisException("WebApplicationContextUtils error");
		}
		return daoSupport;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.dfzc.commons.Service#delete(java.lang.Object)
	 */
	@Transactional
	public void delete(T model) throws EasyIbatisException {
		try {
			getDAOSupport().delete(model);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.dfzc.commons.Service#findAll()
	 */
	public Collection<T> findAll() throws EasyIbatisException {
		try {
			return findAllByFilter(null, null);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.dfzc.commons.Service#findById(java.io.Serializable)
	 */
	public T findById(PK id) throws EasyIbatisException {
		try {
			return (T) getDAOSupport().findById(id);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.dfzc.commons.Service#save(java.lang.Object)
	 */
	@Transactional
	public PK save(T model) throws EasyIbatisException {
		try {
			return (PK) getDAOSupport().save(model);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.dfzc.commons.Service#total()
	 */
	public Long total() throws EasyIbatisException {
		return totalByFilter(null, null);
	}

	/**
	 * 
	 * @param equalsFilter
	 * @param likeFilter
	 * @return
	 * @throws EasyIbatisException
	 */
	public Long totalByFilter(Map<String, ?> equalsFilter, Map<String, ?> likeFilter) throws EasyIbatisException {
		try {
			return getDAOSupport().totalByFilter(equalsFilter, likeFilter);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.dfzc.commons.Service#update(java.lang.Object)
	 */
	@Transactional
	public void update(T model) throws EasyIbatisException {
		try {
			getDAOSupport().update(model);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}
	/**
	 * 
	 * @param equalsFilter
	 * @param likeFilter
	 * @return
	 * @throws EasyIbatisException
	 */
	public Collection<T> findAllByFilter(Map<String, ?> equalsFilter, Map<String, ?> likeFilter, Sort... sorts)
			throws EasyIbatisException {
		try {
			return getDAOSupport().findAllByFilter(equalsFilter, likeFilter, sorts);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}
	/**
	 * 
	 * @param equalsFilter
	 * @param likeFilter
	 * @param start
	 * @param limit
	 * @return
	 * @throws EasyIbatisException
	 */
	public Collection<T> findAllByFilter(Map<String, ?> equalsFilter, Map<String, ?> likeFilter, Integer start,
			Integer limit, Sort... sorts) throws EasyIbatisException {
		try {
			return getDAOSupport().findAllByFilter(equalsFilter, likeFilter, start, limit, sorts);
		} catch (EasyIbatisException e) {
			throw parseException(e);
		}
	}
}
